#include <bits/stdc++.h>
using namespace std;
int n,m;
int as[100005],c[100005];
bool d[1000005];
int base[31];
int a[100005];
int x[5];
int s;
int cnt;
int fans[1000005];
int ff[1000005];
int query(int l,int r,int bj,int R){
	if(l==r){
		if(l>bj)return -1;
		return l;
	}
	int mid=(l+r)>>1;
	int x,y;
	x=query(l,mid,bj,R-1);
	y=query(mid+1,r,bj,R-1);
	cnt++;
	if(x==-1&&y==-1){
		return -1;
	}
	if(d[cnt]==0){
		if(x==-1){
			return -1;
		}
		if(a[x]>=R){
			fans[mid+1]--;
			fans[r+1]++;
			return x;
		}
		else return y;
	}
	else{
		if(y==-1){
			return -1;
		}
		if(a[y]>=R){
			fans[l]--;
			fans[mid+1]++;
			return y;
		}
		else return x;
	}
}
void work(){
	cin>>x[0]>>x[1]>>x[2]>>x[3];
	for(int i=1;i<=n;i++){
		a[i]=as[i]^x[i%4];
	}
	int ans;
	for(int i=1;i<=m;i++){
		int k;
		for(int j=0;j<=30;j++){
			k=j;
			if(base[j]>=c[i])break;
		}
		for(int j=1;j<=base[k];j++){
			fans[j]=0;
		}
		cnt=0;
		//cout<<fans[1]<<" ";
		query(1,base[k],c[i],k);
		s=0;
		//cout<<fans[1]<<" ";
		memset(ff,0,sizeof(ff));
		for(int j=1;j<=base[k];j++){
			ff[j]=ff[j-1]+fans[j];
			//cout<<ff[j]<<" "<<fans[j]<<" ";
			if(ff[j]==0){
				s+=j;
				//cout<<j<<" ";
			}
		}
		if(i==1)ans=s;
		else ans=ans^(s*i);
		//cout<<'\n';
	}
	cout<<ans<<'\n';
}
int main(){
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	base[0]=1;
	for(int i=1;i<=30;i++)base[i]=base[i-1]<<1;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>as[i];
	}
	for(int i=1;i<=m;i++){
		cin>>c[i];
	}
	int k=0;
	for(int i=0;i<=30;i++){
		k=i;
		if(base[i]>=n)break;
	}
	int tot=0;
	char ch;
	for(int i=1;i<=k;i++){
		for(int j=1;j<=base[k-i];j++){
			cin>>ch;
			tot++;
			if(ch=='0')d[tot]=0;
			else d[tot]=1;
		}
	}
	int T;
	cin>>T;
	while(T--){
		work();
	}
	return 0;
}
/*
5 5
0 0 0 0 0
5 4 1 2 3
1001
10
1
4
2 1 0 0
1 2 1 0
0 2 3 1
2 2 0 1
*/
